﻿using PickingGuide.Handle;
using PickingGuide.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Windows.Forms;

namespace PickingGuideBoard2
{
    public partial class Form2 : Form
    {
        PickingGuideHandler pickingGuideHandler;
        int QueueLength;
        /// <summary>
        /// 工位
        /// </summary>
        private int Station = 2;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pickingGuideHandler = PickingGuideHandler.getInstance();
            var systemConfig = pickingGuideHandler.GetSystemConfig();
            //QueueLength = systemConfig.QueueLength;
            QueueLength = 5;

            Station = int.Parse(ConfigurationManager.AppSettings["rack_type"].ToString());

            //设置Timer1控件可用
            this.timer1.Enabled = false;
            //设置Timer1 时间间隔
            this.timer1.Interval = 500;
            this.timer1.Start();

        }

        private void WorkGuideRefresh(QueueInfoModel model)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                //uiPanelBody.Text = model.BodyNum;
                uiPanelModel.Text = model.CarModel;
            }));
        }

        private void PanelRefresh(List<RackSetupModel> rackSetupModels)
        {
            this.Invoke(new MethodInvoker(() =>
                {
                    panelParts.Controls.Clear();
                    var list = rackSetupModels.Where(x => x.Status == "1" || x.Status == "2"||x.Status=="3").ToList();
                    if (list == null || list.Count == 0)
                        return;
                    var count = list.Count();
                    var height = panelParts.Size.Height;
                    var width = panelParts.Size.Width/ count;

                    for(int i = 0;i< count;i++)
                    {
                        UIPanel panel = new UIPanel();
                        panel.RectColor = Color.Black;
                        panel.Location = new Point(width * i, panel.Location.Y);
                        panel.Height = height;
                        panel.Width = width;
                        panel.Text += $"{list[i].Name}\r\n";
                        panel.Text += $"{list[i].Code}\r\n";
                        panel.Font = new Font(panel.Font.FontFamily, 65.0f, panel.Font.Style, panel.Font.Unit);
                        switch(list[i].Status)
                        {
                            case "0":
                                panel.BackColor = Color.White;
                                panel.FillColor = Color.White;
                                break;
                            case "1":
                                panel.BackColor = Color.FromArgb(80, 160, 255);
                                panel.FillColor = Color.FromArgb(80, 160, 255);
                                break;
                            case "2":
                                panel.BackColor = Color.LightGreen;
                                panel.FillColor = Color.LightGreen;
                                break;
                            case "3":
                                panel.BackColor = Color.Red;
                                panel.FillColor = Color.Red;
                                break;
                        }

                        panelParts.Controls.Add(panel);
                    }

                }));
        }

        public void DgvRefresh(List<QueueInfoModel> queue)
        {
            this.Invoke(new MethodInvoker(() =>
            {

            }));
        }

        public void Dgv_Cells(ref UIDataGridView dgv)
        {
            if (dgv.Rows.Count > 0)
                dgv.Rows[0].Selected = false;

            foreach (DataGridViewRow item in dgv.Rows)
            {
                var model = item.DataBoundItem as QueueInfoModel;
                if (model.Status == "ok")
                {
                    item.DefaultCellStyle.BackColor = Color.LightGreen;
                }
                else if (model.Status == "work")
                {
                    item.DefaultCellStyle.BackColor = Color.Yellow;
                }
                else if (model.Status == "unok")
                {
                    item.DefaultCellStyle.BackColor = Color.Red;
                }
                else if (model.Status == "pass")
                {
                    item.DefaultCellStyle.BackColor = Color.LightGray;
                }
                else
                {
                    item.DefaultCellStyle.BackColor = Color.White;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                var model = pickingGuideHandler.GetWorkInfoToWeb(Station);
                if (model != null)
                {
                    var racks = pickingGuideHandler.GetRackList(model, Station);
                    if (racks != null && racks.Count > 0)
                    {

                        WorkGuideRefresh(model);
                        PanelRefresh(racks);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.Close();
        }
    }
}
